<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Request;
use think\facade\View;
use think\facade\Json;
use app\common\model\Transfer as TransferModel;

class Transfer extends Base
{
    //
    public function index(){
        if(request()->isAjax()){
            $res = model('transfer')->save(input('post.'));
            if($res){
                $this->success('添加成功！',$_SERVER['HTTP_REFERER']);
            }else{
                $this->error('添加失败！');
            }
        }
        return view();
    }

    public function lst(){
        $where   = [];
        $keyword = Request::param('keyword');
        if (!empty($keyword)) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }
        $dateran = Request::param('dateran');
        if (!empty($dateran)) {
            $getDateran = get_dateran($dateran);
            $where[]    = ['create_time', 'between', $getDateran];
        }
        $list = TransferModel::getList($where, $this->pageSize, ['id' => 'desc']);
        $all_articleNum = model('transfer')->select();
        $view = [
            'keyword'  => $keyword,
            'dateran'  => $dateran,
            'pageSize' => page_size($this->pageSize, $list->total()),
            'page'     => $list->render(),
            'list'     => $list,
            'empty'    => empty_list(9),
            'all_order_num' => $all_articleNum
        ];
        View::assign($view);
        return view();
    }

      //删除
   public function del()
   {
       $id = Request::param('id');
       TransferModel::destroy($id);
       return json(['error' => 0, 'msg' => '删除成功']);
   }

   //批量删除
   public function selectDel()
   {
       $id = Request::param('id');
       TransferModel::destroy($id);
       return json(['error' => 0, 'msg' => '删除成功!']);
   }

   public function edit($id=0){
    if(request()->isAjax()){
        $data = input('post.');
        $result = model('transfer')->where('id',input('post.id'))->update($data);
        if($result)
         {
             $this->success('修改成功',$_SERVER['HTTP_REFERER']);
         }else{
             $this->error('修改失败!',$_SERVER['HTTP_REFERER']);
         }
    }
    $orderInfo = model('transfer')->find($id);
    $viewData = [
        'order'=>$orderInfo
    ];
    $this->assign($viewData);
    return view();
}
}
